﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CourseManager.Domain.Entities;
using CourseManager.Domain.Repositories;
namespace CourseManager.WebUI.Controllers
{
    public class InstructorController : Controller
    {
        InstructorRepository SR;

        [HttpGet]
        public ActionResult GetInstructor()
        {
            return View();
        }

        [HttpPost]
        public ActionResult GetInstructor(FormCollection pName)
        {
            SR = new InstructorRepository();
            IEnumerable<Instructor> InstructorList;


            if (pName["Name"].Trim().Length == 0)
            {
                ModelState.AddModelError("Name", "Requerido");
                ViewData["InstructorList"] = GetInstructor();
                return View();

            }

            InstructorList = SR.GetInstructorByName(pName["Name"].ToString());
            return View(InstructorList);
        }
        
        [HttpGet]
        public ActionResult AddInstructor()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AddInstructor(FormCollection pInstructor)
        {
            Instructor Instructor = new Instructor();
            SR = new InstructorRepository();

            if (pInstructor["Name"].Trim().Length == 0)
            {
                ModelState.AddModelError("Name", "Requerido");
                return View();

            }
            else if (pInstructor["LastName"].Trim().Length == 0)
            {
                ModelState.AddModelError("LastName", "Requerido");
                return View();

            }
            else if (pInstructor["Telephone"].Trim().Length == 0)
            {
                ModelState.AddModelError("Telephone", "Requerido");
                return View();

            }
            else if (pInstructor["Cellphone"].Trim().Length == 0)
            {
                ModelState.AddModelError("Cellphone", "Requerido");
                return View();

            }
            else if (pInstructor["Email"].Trim().Length == 0)
            {
                ModelState.AddModelError("Email", "Requerido");
                return View();

            }
            else if (pInstructor["Address"].Trim().Length == 0)
            {
                ModelState.AddModelError("Address", "Requerido");
                return View();

            }

            Instructor.Name = pInstructor["Name"].ToString();
            Instructor.LastName = pInstructor["LastName"].ToString();
            Instructor.Telephone = pInstructor["Telephone"].ToString();
            Instructor.Cellphone = pInstructor["Cellphone"].ToString();
            Instructor.Email = pInstructor["Email"].ToString();
            Instructor.Address = pInstructor["Address"].ToString();

            SR.AddInstructor(Instructor);

            return View();
        }

        public bool DeleteInstructor(int pId)
        {
            SR = new InstructorRepository();
            SR.DeleteInstructor(pId);
            return true;
        }



    }
}
